<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>


    <el-table
      v-loading="loading"
      :data="dqnoticeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="内容" align="center" prop="content">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="showDetail(scope.row.content)"
            >预览</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="filePath">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="downloadFile(scope.row.filePath)"
            >下载</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="发送人" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
            <el-button
            size="mini"
            type="text"
            @click="confirmNotice(scope.row.noticeId)"
            >确认通知</el-button
          >
  
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通知列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>

        <el-form-item label="附件" prop="filePath">
          <el-upload
            class="upload-demo"
            :action="uploadImgUrl"
            :headers="headers"
            :on-success="quillImgSuccess"
            :on-error="uploadError"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="内容">
          <Editor v-model="form.content" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="信息"
      :visible.sync="detailOpen"
      width="800px"
      append-to-body
    >
      <el-row class="content" v-html="content"></el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  myListNotice
} from "@/api/system/dqnotice";
import{confirmNotice} from  "@/api/system/userNotice";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";
export default {
  name: "Dqnotice",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      detailOpen: false,
      content: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      // 总条数
      total: 0,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload",
      // 通知列表表格数据
      dqnoticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        content: undefined,
        filePath: undefined,
        createBy: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询通知列表列表 */
    getList() {
      this.loading = true;
      myListNotice(this.queryParams).then((response) => {
        this.dqnoticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
   confirmNotice(noticeId){
      confirmNotice(noticeId).then(res=>{
        this.msgSuccess("您已确认通知");
      })
    },
    showDetail(content) {
      this.content = content;
      this.detailOpen = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        title: undefined,
        content: undefined,
        filePath: undefined,
        createBy: undefined,
        createTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.noticeId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加通知列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const noticeId = row.noticeId || this.ids;
      getDqnotice(noticeId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改通知列表";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.noticeId != undefined) {
            updateDqnotice(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addDqnotice(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId || this.ids;
      this.$confirm(
        '是否确认删除通知列表编号为"' + noticeIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delDqnotice(noticeIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },

    quillImgSuccess(res, file) {
      // 如果上传成功
      if (res.code == 200) {
        this.form.filePath = res.fileName;
      } else {
        this.$message.error("上传失败");
      }
    },
    // 富文本图片上传失败
    uploadError() {
      // loading动画消失
      this.$message.error("上传失败");
    },
    downloadFile(path) {
      window.open(process.env.VUE_APP_BASE_API + path);
    },
  },
};
</script>